package clientGUI.studentGUI;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.*;
import javax.swing.*;

import clientApp.VirtualAdvisor;
import clientGUI.PassBackToable;
import clientGUI.SelectCoursesGUI_Catalog;


public class RegisterGUI implements PassBackToable{
	private JFrame registerFrame;
	private boolean ready;
	private VirtualAdvisor myVA;
	
	public RegisterGUI(VirtualAdvisor VA) throws InterruptedException, IOException
	{		
		ready = false;
		myVA = VA;
		JPanel registerPane = createPaneREGISTER();
		registerFrame = GUI_Function_Library.displayPane ("Register User Account", registerPane, true);
	}

	public boolean ready()
	{
		return ready;
	}
	
	/**
	 * interface with something to register the username and password
	 * @param username
	 * @param password
	 * @throws Exception 
	 */
	private void registerUser(String username, String password, String name, String degree) throws Exception
	{
		boolean taken = myVA.checkUser(username, password);
		myVA.registerAndlogin(username, password, name, degree);
		
		// check whether the username is taken
		// create account
		if(!taken)
		{
			GUI_Function_Library.undisplayFrame(registerFrame);			
//			AddCoursesGUI enterTranscriptConsole = new AddCoursesGUI(this, false, "Enter Transcript", "Add all the courses you have taken to date", null, myVA);
			SelectCoursesGUI_Catalog enterTranscriptConsole = new SelectCoursesGUI_Catalog(this, "register_user", false, null, myVA);
//			while(enterTranscriptConsole.ready())
//			{
//				Thread.sleep(100);
//			}
//			HashSet<String> courses = enterTranscriptConsole.coursesSelected();
		}
		else
			throw new Exception("Username is already in use.  Please use a different username.");
		// open enter transcript window
		
	}
	

	/**
	 * NEEDS STRING[] WITH DEGREES
	 * @return
	 */
	private JPanel createPaneREGISTER() throws InterruptedException, IOException
	{
		JPanel usernamePane		= new JPanel(new GridLayout(1,2));
		JLabel usernameLabel	= new JLabel("Username");
		final JTextField usernameField = new JTextField(10);
		usernamePane.add(usernameLabel);
		usernamePane.add(usernameField);
		
		JPanel passwordPane		= new JPanel(new GridLayout(1,2));
		JLabel passwordLabel	= new JLabel("Password");
		final JTextField passwordField = new JTextField(10);
		passwordPane.add(passwordLabel);
		passwordPane.add(passwordField);
		
		JPanel namePane		= new JPanel(new GridLayout(1,2));
		JLabel nameLabel	= new JLabel("Name");
		final JTextField nameField = new JTextField(10);
		namePane.add(nameLabel);
		namePane.add(nameField);
		
		JPanel degreePane		= new JPanel(new GridLayout(1,2));
		JLabel degreeLabel		= new JLabel("Degree Program");
		String[] degreeOptions	= {"Computer Science(CS) Major", "Information Systems(IS) Major", "CS/IS Double Major", "IS Major/ CS Minor", "Undecided"};
		final JComboBox degreeDropdown = new JComboBox(degreeOptions);
		degreePane.add(degreeLabel);
		degreePane.add(degreeDropdown);
		
		JButton registerButton	= new JButton("Register");
		registerButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				//TODO: error checking
				ready = true;
				String username = usernameField.getText();
				String password = passwordField.getText();
				String name = nameField.getText();
				String degree	= (String)degreeDropdown.getSelectedItem();
				try {
					registerUser(username, password, name, degree);
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(null, e1.getMessage());
				}	
			}
		});
		
		
		JPanel registerPane = new JPanel(new GridLayout(4,1));
		registerPane.add(usernamePane);
		registerPane.add(passwordPane);
		registerPane.add(namePane);
		registerPane.add(degreePane);
		registerPane.add(registerButton);
		
		System.out.println("here");

		return registerPane;
	}

	@Override
	public void passBack(String cmd, String message) {
		// TODO Auto-generated method stub
		System.out.println("REGISTER GUI PASSBACKABLE: " + cmd + " --> " + message);
		JOptionPane.showMessageDialog(null, "NEEDS BACK IMPLEMENTATION: REGISTER USER");
	}
	
}
